package com.szpx.service.impl;

import com.szpx.entity.XWY.XWYDenglu;
import com.szpx.mapper.XWYDengluMapper;
import com.szpx.service.XWYZhuceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * XWY注册服务实现类
 */
@Service
public class XWYZhuceServiceImpl implements XWYZhuceService {

    @Autowired
    private XWYDengluMapper xwyDengluMapper;

    /**
     * 用户注册
     * @param user 用户信息
     * @return 注册结果
     */
    @Override
    public Map<String, Object> register(XWYDenglu user) {
        Map<String, Object> result = new HashMap<>();
        
        // 检查用户名是否已存在
        XWYDenglu existingUser = xwyDengluMapper.selectByUsername(user.getUsername());
        if (existingUser != null) {
            result.put("success", false);
            result.put("message", "用户名已存在");
            return result;
        }
        
        // 检查手机号是否已存在
        existingUser = xwyDengluMapper.selectByPhone(user.getPhone());
        if (existingUser != null) {
            result.put("success", false);
            result.put("message", "手机号已被注册");
            return result;
        }
        
        // 设置创建时间
        user.setCreateTime(new Date());
        
        // 设置默认状态为1（正常）
        user.setStatus(1);
        
        // 密码加密
        String md5Password = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        user.setPassword(md5Password);
        
        // 保存用户
        try {
            xwyDengluMapper.insert(user);
            result.put("success", true);
            result.put("message", "注册成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "注册失败：" + e.getMessage());
        }
        
        return result;
    }

    /**
     * 检查用户名是否可用
     * @param username 用户名
     * @return 检查结果
     */
    @Override
    public Map<String, Object> checkUsername(String username) {
        Map<String, Object> result = new HashMap<>();
        
        XWYDenglu user = xwyDengluMapper.selectByUsername(username);
        if (user != null) {
            result.put("available", false);
            result.put("message", "用户名已存在");
        } else {
            result.put("available", true);
            result.put("message", "用户名可用");
        }
        
        return result;
    }

    /**
     * 检查手机号是否可用
     * @param phone 手机号
     * @return 检查结果
     */
    @Override
    public Map<String, Object> checkPhone(String phone) {
        Map<String, Object> result = new HashMap<>();
        
        XWYDenglu user = xwyDengluMapper.selectByPhone(phone);
        if (user != null) {
            result.put("available", false);
            result.put("message", "手机号已被注册");
        } else {
            result.put("available", true);
            result.put("message", "手机号可用");
        }
        
        return result;
    }
}